
package com.idealighter.game.games.fqzs.struct;

import com.idealighter.game.core.common.Game;
import com.idealighter.game.dictionary.domain.SharkRoomDomain;
import com.idealighter.game.games.common.AbstractRoom;
import com.idealighter.game.games.common.AbstractSeat;
import com.idealighter.game.games.common.AbstractTable;
import com.idealighter.game.games.common.InstanceState;
import com.idealighter.game.games.fqzs.manager.SharkDataMgr;
import com.idealighter.game.server.context.ApplicationContext;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * 飞禽走兽房间 .
 * 
 * @date 2016年2月17日 上午11:55:32
 *
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class SharkRoom extends AbstractRoom {

  private final Map<Integer, SharkTable> tables;

  private final int instanceId;

  private SharkRoomDomain roomDomain;

  private InstanceState instanceState;

  /**
   * 构造函数 .
   * 
   * @param instanceId 实例id .
   * @param roomDom .
   */
  public SharkRoom(int instanceId, SharkRoomDomain roomDom) {
    super(roomDom.getId(), roomDom.getName());
    this.instanceState = InstanceState.NORMAL;
    this.instanceId = instanceId;
    this.roomDomain = roomDom;
    this.tables =
        createTables(roomDom.getTableNum(), roomDom.getChair(), roomDom.getBankerApplyNums());
  }

  /**
   * 创建桌子 .
   * 
   * @param tableNum 桌子人数.
   * @param chairNum 椅子人数.
   * @param applyBankerNum 申请庄家人数.
   * @return 创建桌子.
   */
  private Map<Integer, SharkTable> createTables(int tableNum, int chairNum, int applyBankerNum) {
    Map<Integer, SharkTable> tables = new HashMap<>();
    for (int i = 0; i < tableNum; i++) {
      int tableId = id.intValue() * AbstractTable.MAX_TABLE + i + 1;
      tables.put(tableId, new SharkTable(tableId, this, chairNum, applyBankerNum));
    }

    return tables;
  }

  public SharkTable removeTable(int tableId) {
    return tables.remove(tableId);
  }

  @Override
  public Game game() {
    return Game.FQZS;
  }

  @Override
  public AbstractSeat seat(long playerId) {
    return ApplicationContext.getBean(SharkDataMgr.class).getPlayerSeat(playerId);
  }

  @Override
  public Collection<SharkTable> tables() {
    return tables.values();
  }


}
